using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[AddComponentMenu("MaterialUI/Progress/Linear Progress Indicator")]
	public class LinearProgressIndicator : ProgressIndicator
	{
		[SerializeField]
		private RectTransform m_BarRectTransform;

		private Image m_BarImage;

		[SerializeField]
		private Image m_BackgroundImage;

		private const float animDuration = 0.65f;

		private int m_AnimBarPos;

		private float m_AnimBarPosStartTime;

		private float m_AnimBarPosCurrentPos;

		private bool m_AnimColor;

		private float m_AnimColorStartTime;

		private Color m_AnimColorCurrentColor;

		private Color m_AnimColorTargetColor;

		private bool m_AnimSize;

		private float m_AnimSizeStartTime;

		private float m_AnimSizeCurrentSize;

		private float m_AnimSizeTargetSize;

		private int m_AnimBar;

		private float m_AnimBarStartTime;

		private float m_AnimBarCurrentSize;

		private float m_AnimBarTargetSize;

		private float m_AnimBarCurrentPos;

		private float m_BarLength;

		public RectTransform barRectTransform
		{
			get
			{
				return m_BarRectTransform;
			}
			set
			{
				m_BarRectTransform = value;
			}
		}

		public Image barImage
		{
			get
			{
				if (m_BarImage == null && m_BarRectTransform != null)
				{
					m_BarImage = m_BarRectTransform.GetComponent<Image>();
				}
				return m_BarImage;
			}
		}

		public Image backgroundImage
		{
			get
			{
				return m_BackgroundImage;
			}
			set
			{
				m_BackgroundImage = value;
			}
		}

		private void Start()
		{
			if (Application.isPlaying)
			{
				if (m_StartsHidden)
				{
					base.scaledRectTransform.localScale = new Vector3(1f, 0f, 1f);
				}
				else if (m_StartsIndeterminate)
				{
					StartIndeterminate();
				}
			}
		}

		private void Update()
		{
			UpdateAnimSize();
			UpdateAnimColor();
			UpdateAnimBar();
		}

		private void UpdateAnimSize()
		{
			if (base.scaledRectTransform == null || !m_AnimSize)
			{
				return;
			}
			float num = Time.realtimeSinceStartup - m_AnimSizeStartTime;
			if (num < 0.65f)
			{
				Vector3 localScale = base.scaledRectTransform.localScale;
				localScale.y = Tween.CubeInOut(m_AnimSizeCurrentSize, m_AnimSizeTargetSize, num, 0.65f);
				base.scaledRectTransform.localScale = localScale;
				return;
			}
			Vector3 localScale2 = base.scaledRectTransform.localScale;
			localScale2.y = m_AnimSizeTargetSize;
			base.scaledRectTransform.localScale = localScale2;
			m_AnimSize = false;
			if (m_AnimSizeTargetSize == 0f)
			{
				base.gameObject.SetActive(value: false);
			}
		}

		private void UpdateAnimColor()
		{
			if (!(m_BackgroundImage == null) && m_AnimColor)
			{
				float num = Time.realtimeSinceStartup - m_AnimColorStartTime;
				if (num < 0.65f)
				{
					barImage.color = Tween.CubeInOut(m_AnimColorCurrentColor, m_AnimColorTargetColor, num, 0.65f);
					m_BackgroundImage.color = barImage.color;
				}
				else
				{
					barImage.color = m_AnimColorTargetColor;
					m_BackgroundImage.color = barImage.color;
					m_AnimColor = false;
				}
			}
		}

		private void UpdateAnimBar()
		{
			if (base.rectTransform == null || m_AnimBar == 0)
			{
				return;
			}
			float num = Time.realtimeSinceStartup - m_AnimBarStartTime;
			Vector2 properSize = base.rectTransform.GetProperSize();
			m_BarLength = properSize.x;
			if (m_AnimBar == 1)
			{
				if (num < 0.65f)
				{
					Vector2 vector = m_BarRectTransform.sizeDelta;
					vector.x = Tween.CubeIn(m_AnimBarCurrentSize, m_BarLength / 2f, num, 0.65f);
					m_BarRectTransform.sizeDelta = vector;
					vector = m_BarRectTransform.anchoredPosition;
					vector.x = Tween.CubeIn(m_AnimBarCurrentPos, m_BarLength / 4f, num, 0.65f);
					m_BarRectTransform.anchoredPosition = vector;
					return;
				}
				Vector2 vector2 = m_BarRectTransform.sizeDelta;
				vector2.x = m_BarLength / 2f;
				m_BarRectTransform.sizeDelta = vector2;
				vector2 = m_BarRectTransform.anchoredPosition;
				vector2.x = m_BarLength / 4f;
				m_BarRectTransform.anchoredPosition = vector2;
				Vector2 sizeDelta = m_BarRectTransform.sizeDelta;
				m_AnimBarCurrentSize = sizeDelta.x;
				Vector2 anchoredPosition = m_BarRectTransform.anchoredPosition;
				m_AnimBarCurrentPos = anchoredPosition.x;
				m_AnimBarStartTime = Time.realtimeSinceStartup;
				m_AnimBar = 2;
			}
			else if (m_AnimBar == 2)
			{
				if (num < 0.65f)
				{
					Vector2 vector3 = m_BarRectTransform.sizeDelta;
					vector3.x = Tween.CubeOut(m_AnimBarCurrentSize, 0f, num, 0.65f);
					m_BarRectTransform.sizeDelta = vector3;
					vector3 = m_BarRectTransform.anchoredPosition;
					vector3.x = Tween.CubeOut(m_AnimBarCurrentPos, m_BarLength, num, 0.65f);
					m_BarRectTransform.anchoredPosition = vector3;
					return;
				}
				Vector2 vector4 = m_BarRectTransform.sizeDelta;
				vector4.x = 0f;
				m_BarRectTransform.sizeDelta = vector4;
				vector4 = m_BarRectTransform.anchoredPosition;
				vector4.x = 0f;
				m_BarRectTransform.anchoredPosition = vector4;
				Vector2 sizeDelta2 = m_BarRectTransform.sizeDelta;
				m_AnimBarCurrentSize = sizeDelta2.x;
				Vector2 anchoredPosition2 = m_BarRectTransform.anchoredPosition;
				m_AnimBarCurrentPos = anchoredPosition2.x;
				m_AnimBarStartTime = Time.realtimeSinceStartup;
				m_AnimBar = 1;
			}
			else if (m_AnimBar == 3)
			{
				if (num < 0.65f && Application.isPlaying)
				{
					Vector2 vector5 = m_BarRectTransform.sizeDelta;
					vector5.x = Tween.CubeInOut(m_AnimBarCurrentSize, m_AnimBarTargetSize, num, 0.65f);
					m_BarRectTransform.sizeDelta = vector5;
					vector5 = m_BarRectTransform.anchoredPosition;
					vector5.x = Tween.CubeInOut(m_AnimBarCurrentPos, 0f, num, 0.65f);
					m_BarRectTransform.anchoredPosition = vector5;
				}
				else
				{
					Vector2 vector6 = m_BarRectTransform.sizeDelta;
					vector6.x = m_AnimBarTargetSize;
					m_BarRectTransform.sizeDelta = vector6;
					vector6 = m_BarRectTransform.anchoredPosition;
					vector6.x = 0f;
					m_BarRectTransform.anchoredPosition = vector6;
					m_AnimBar = 0;
				}
			}
		}

		public override void Show(bool startIndeterminate = true)
		{
			if (!(base.scaledRectTransform == null))
			{
				base.gameObject.SetActive(value: true);
				Vector3 localScale = base.scaledRectTransform.localScale;
				m_AnimSizeCurrentSize = localScale.y;
				m_AnimSizeTargetSize = 1f;
				m_AnimSizeStartTime = Time.realtimeSinceStartup;
				m_AnimSize = true;
				if (!m_IsAnimatingIndeterminate && startIndeterminate)
				{
					StartIndeterminate();
				}
			}
		}

		public override void Hide()
		{
			if (!(base.scaledRectTransform == null))
			{
				Vector3 localScale = base.scaledRectTransform.localScale;
				m_AnimSizeCurrentSize = localScale.y;
				m_AnimSizeTargetSize = 0f;
				m_AnimSizeStartTime = Time.realtimeSinceStartup;
				m_AnimSize = true;
			}
		}

		public override void StartIndeterminate()
		{
			if (!(barRectTransform == null) && !(base.rectTransform == null))
			{
				Vector2 properSize = base.rectTransform.GetProperSize();
				m_BarLength = properSize.x;
				m_IsAnimatingIndeterminate = true;
				Vector2 sizeDelta = m_BarRectTransform.sizeDelta;
				m_AnimBarCurrentSize = sizeDelta.x;
				Vector2 anchoredPosition = m_BarRectTransform.anchoredPosition;
				m_AnimBarCurrentPos = anchoredPosition.x;
				m_AnimBarStartTime = Time.realtimeSinceStartup;
				m_AnimBar = 2;
				Show();
			}
		}

		public override void SetProgress(float progress, bool animated = true)
		{
			if (!(barRectTransform == null) && !(base.rectTransform == null))
			{
				progress = Mathf.Clamp(progress, 0f, 1f);
				Vector2 properSize = base.rectTransform.GetProperSize();
				m_BarLength = properSize.x;
				m_IsAnimatingIndeterminate = false;
				if (!animated)
				{
					Vector2 vector = m_BarRectTransform.sizeDelta;
					vector.x = m_BarLength * progress;
					m_BarRectTransform.sizeDelta = vector;
					vector = m_BarRectTransform.anchoredPosition;
					vector.x = 0f;
					m_BarRectTransform.anchoredPosition = vector;
					m_AnimBar = 0;
				}
				else
				{
					Vector2 sizeDelta = m_BarRectTransform.sizeDelta;
					m_AnimBarCurrentSize = sizeDelta.x;
					Vector2 anchoredPosition = m_BarRectTransform.anchoredPosition;
					m_AnimBarCurrentPos = anchoredPosition.x;
					m_AnimBarTargetSize = m_BarLength * progress;
					m_AnimBarStartTime = Time.realtimeSinceStartup;
					m_AnimBar = 3;
				}
			}
		}

		public override void SetColor(Color color)
		{
			if (!(barImage == null))
			{
				m_AnimColorCurrentColor = barImage.color;
				m_AnimColorTargetColor = color;
				m_AnimColorStartTime = Time.realtimeSinceStartup;
				m_AnimColor = true;
			}
		}

		public override float GetMinWidth()
		{
			return 24f;
		}

		public override float GetMinHeight()
		{
			return 4f;
		}
	}
}
